package org.linlinjava.litemall.admin.web;

import com.github.pagehelper.PageInfo;
import com.github.pagehelper.util.StringUtil;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.linlinjava.litemall.admin.annotation.LoginAdmin;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.util.bcrypt.BCryptPasswordEncoder;
import org.linlinjava.litemall.db.domain.LitemallMechanismUser;
import org.linlinjava.litemall.db.service.LitemallMechanismUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/mechanismUser")
public class LitemallMechanismUserController {
    private final Log logger = LogFactory.getLog(LitemallMechanismUserController.class);
    @Autowired
    private LitemallMechanismUserService litemallMechanismUserService;


    @GetMapping("/list")
    public Object list(@LoginAdmin Integer adminId,
                       String username, String fullName,
                       Integer mechanismId,
                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                       @RequestParam(value = "limit", defaultValue = "10") Integer limit,
                       String sort, String order){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        List<LitemallMechanismUser> adList = litemallMechanismUserService.querySelective(mechanismId,username, fullName, page, limit, sort, order);
        long total = PageInfo.of(adList).getTotal();
        Map<String, Object> data = new HashMap<>();
        data.put("total", total);
        data.put("items", adList);

        return ResponseUtil.ok(data);
    }

    @GetMapping("/username")
    public Object username(String status, Integer id, String username) {
        if (StringUtil.isEmpty(username)) {
            return ResponseUtil.badArgument();
        }
        int total = litemallMechanismUserService.countSeletive(status, id, username);
        if (total == 0) {
            return ResponseUtil.ok(true);
        }
        return ResponseUtil.ok(false);
    }

    @PostMapping("/create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismUser litemallMechanismUser){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        litemallMechanismUser.setAddTime(LocalDateTime.now());

        String rawPassword = litemallMechanismUser.getPassword();

        String username = litemallMechanismUser.getUsername();
        username.replaceAll(" ", "");
        litemallMechanismUser.setUsername(username);
        String fullName = litemallMechanismUser.getFullName();
        fullName.replaceAll(" ", "");
        litemallMechanismUser.setFullName(fullName);
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        String encodedPassword = encoder.encode(rawPassword);
        litemallMechanismUser.setPassword(encodedPassword);
        litemallMechanismUserService.add(litemallMechanismUser);
        return ResponseUtil.ok(litemallMechanismUser);
    }

    @GetMapping("/read")
    public Object read(@LoginAdmin Integer adminId, Integer id){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        if(id == null){
            return ResponseUtil.badArgument();
        }

        LitemallMechanismUser brand = litemallMechanismUserService.findById(id);
        return ResponseUtil.ok(brand);
    }

    @PostMapping("/update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismUser ad){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }

        String rawPassword = ad.getPassword();
        if (rawPassword != null && !rawPassword.equals("")) {
            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String encodedPassword = encoder.encode(rawPassword);
            ad.setPassword(encodedPassword);
        }

        String username = ad.getUsername();
        username.replaceAll(" ", "");
        ad.setUsername(username);
        String fullName = ad.getFullName();
        fullName = fullName.replaceAll(" ", "");
        ad.setFullName(fullName);
        litemallMechanismUserService.update(ad);
        return ResponseUtil.ok(ad);
    }

    @PostMapping("/delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody LitemallMechanismUser ad){
        if(adminId == null){
            return ResponseUtil.unlogin();
        }
        litemallMechanismUserService.deleteById(ad.getId());
        return ResponseUtil.ok();
    }
}
